package com.example.restaurante_admin;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends Activity {

	EditText usuario;
	EditText senha;
	RadioGroup tipo;
	Button logar;
	TextView mensagem;
	boolean tipo_selecionado = false;
	boolean garcom = false;
	boolean cozinha = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		usuario = (EditText)findViewById(R.id.amEtUsuario);
		senha = (EditText)findViewById(R.id.amEtSenha);
		tipo = (RadioGroup)findViewById(R.id.amRgTipo);
		mensagem = (TextView)findViewById(R.id.amTvMensagem);
		logar = (Button)findViewById(R.id.amBtLogar);
		
		tipo.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				garcom = R.id.amRbGarcom == checkedId;
				cozinha = R.id.amRbCozinha == checkedId;
				if((garcom && !cozinha) || (!garcom && cozinha)){
					tipo_selecionado = true;
				}
			}
		});
		
		logar.setOnClickListener(new Button.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String msg = new String();
				msg = "";
				boolean validar = false;
				if(usuario.getText().toString().equals("") || usuario.getText().toString() == ""){					
					msg += getString(R.string.valInformarUsuario)+"\n";
					validar = true;
				}
				if(senha.getText().toString().equals("") || senha.getText().toString() == ""){
					msg += getString(R.string.valInformarSenha)+"\n";
					validar = true;
				}				
				if(!tipo_selecionado){
					msg += getString(R.string.valSelecionarTipo)+"\n";
					validar = true;
				}
				if(validar){
					mensagem.setText(msg.toString());
				}else{
					SoapObject soap = new SoapObject("http://192.168.43.122/RestauranteAndroid/", "login");
					soap.addProperty("login", usuario.getText().toString());
					soap.addProperty("senha", senha.getText().toString());
					SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
					envelope.setOutputSoapObject(soap);
					String url = "http://192.168.43.122/restauranteandroid/servidor.php";
					HttpTransportSE httpTransport = new HttpTransportSE(url);
					Object retorno = null;
					try {
						httpTransport.call("", envelope);
						retorno = envelope.getResponse();
						Log.e("restaurante", "erro "+retorno.toString());
						if(retorno.toString().equals("OK")){
							Intent i = new Intent(getApplication(),null);
							Bundle b = new Bundle();
							b.putInt("garcom_id", 1);
							i.putExtras(b);
							startActivity(i);							
							mensagem.setText("usuario ok.");
						}else{
							mensagem.setText(getString(R.string.valLoginInvalido));
						}
					} catch (Exception e) {
						Log.e("restaurante", "erro "+e);
					}
					
				}
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	

}
